package org.example.studentsdemo.student.controller;

import org.example.studentsdemo.student.entity.Students;
import org.example.studentsdemo.student.service.StudentService;
import org.example.studentsdemo.utils.HttpResult;
import org.example.studentsdemo.utils.ResultCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/students")
public class StudentController {

    @Autowired
    private StudentService studentService;

    /**
     * 获取所有学员列表
     * @return
     */
    @GetMapping
    public ResponseEntity<HttpResult<List<Students>>> getAllStudents() {
        List<Students> students = studentService.getAllStudents();
        if (!students.isEmpty()) {
            return new ResponseEntity<>(HttpResult.success(students), HttpStatus.OK);
        } else {
            return new ResponseEntity<>(HttpResult.failure(ResultCodeEnum.FAILURE), HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }


    /**
     * 根据id查询学员信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public ResponseEntity<HttpResult<Students>> getStudentById(@PathVariable Long id) {
        Students student = studentService.getStudentById(id);
        System.out.println(student);
        if (student != null) {
            return new ResponseEntity<>(HttpResult.success(student), HttpStatus.OK);
        } else {
            return new ResponseEntity<>(HttpResult.failure(ResultCodeEnum.FAILURE), HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    /**
     * 添加学员
     * @param students
     * @return
     */
    @PostMapping("/addStudent")
    public ResponseEntity<HttpResult<Students>> addStudent(@RequestBody Students students) {
        System.out.println("Received student: " + students);
       Students saveStudent =  studentService.saveStudent(students);
       if (saveStudent != null) {
           return new ResponseEntity<>(HttpResult.success(saveStudent), HttpStatus.OK);
       }else{
           return new ResponseEntity<>(HttpResult.failure(ResultCodeEnum.FAILURE), HttpStatus.INTERNAL_SERVER_ERROR);

       }
    }

    /**
     * 更新学员信息
     * @param students
     * @return
     */
    @PutMapping("/updateStudent")
    public ResponseEntity<HttpResult<Students>> updateStudent(@RequestBody Students students) {
        Students updateStudentData = studentService.updateStudent(students);
        if (updateStudentData != null){
            return new ResponseEntity<>(HttpResult.success(updateStudentData),HttpStatus.CREATED);
        }else{
            return new ResponseEntity<>(HttpResult.failure(ResultCodeEnum.FAILURE), HttpStatus.INTERNAL_SERVER_ERROR);

        }

    }

    /**
     * 删除学员信息
     * @param id
     * @return
     */
    @DeleteMapping("/deleteStudent/{id}")
    public ResponseEntity <HttpResult<Boolean>> deleteStudent(@PathVariable Long id) {
      Boolean success =  studentService.deleteStudent(id);
      if (success){
          return new ResponseEntity<>(HttpResult.success(),HttpStatus.OK);
      }else{
          return new ResponseEntity<>(HttpResult.failure(ResultCodeEnum.FAILURE),HttpStatus.INTERNAL_SERVER_ERROR);
      }

    }
}